rm(list = ls())
here::i_am(file.path("code", "15_ols_board_comparison.R"))
library(here)
source(here("code", "config.R"))

est_df <- read_parquet(here("data", "analysis", "analysis.parquet"))

res <- feols(
  c(exemption, married_cards, laborer, farmer, farm_laborer) ~ diff_bw_z +
    sw0(pop_county1920_cards + mh_farm_county1920_cards + mrural_county1920_cards + mliterate_county1920_cards),
  data = est_df |>
    mutate(
      across(c(diff_bw), list(z = \(x) scale(x)[,1]))
    ) |>
    filter(vet_combined == TRUE),
  cluster = ~ board_identifier
)

p_color <- coeftable(res) |>
  filter(coefficient == "diff_bw_z") |>
  mutate(
    lhs = factor(
      lhs,
      levels = c("exemption", "married_cards", "laborer", "farmer", "farm_laborer"),
      labels = c("Exemption", "Married", "Laborer", "Farmer", "Farm laborer")
    ),
    rhs = factor(
      rhs,
      levels = c("diff_bw_z", "diff_bw_z + pop_county1920_cards + mh_farm_county1920_cards + mrural_county1920_cards + mliterate_county1920_cards"),
      labels = c("Baseline", "County controls")
    )
  ) |>
  ggplot(aes(y = fct_rev(lhs), xmin = Estimate - 1.96*`Std. Error`,
             xmax = Estimate + 1.96*`Std. Error`, x = Estimate, color = rhs, shape = rhs)) +
  geom_vline(xintercept = 0, linetype = "dashed", alpha = .7) +
  geom_point(size = 2, position = position_dodge2(.5, reverse=TRUE)) +
  geom_linerange(position = position_dodge2(.5, reverse=TRUE)) +
  my_theme() +
  xlim(-.2, .2) +
  labs(x ="Coefficient on board induction rate gap", y = "", color = "", shape = "")
ggsave(file.path(fig_dir, "color", "ols_board_comparison.pdf"), plot = p_color, width = 5, height = 3)

p_bw <- p_color + scale_color_manual(values = c(Baseline = "black", "County controls" = "#555"))
ggsave(file.path(fig_dir, "bw", "ols_board_comparison.pdf"), plot = p_bw, width = 5, height = 3)
